<template>
    <div class="main">
      <div class="table">
          <div class="chhead">
            <el-breadcrumb separator="/">
              <el-breadcrumb-item :to="{ path: '/managemain' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            </el-breadcrumb>
            <div class="input">
                <el-input
                v-model="search"  @click="handleEdit(scope.$index, scope.row)"
                size="mini"
                prefix-icon="el-icon-search"
                placeholder="输入关键字搜索" class="input" />
            </div>
          </div>
          <el-table
            :data="tableData.filter(data => !search || data.username.toLowerCase().includes(search.toLowerCase()))"
            style="width: 100%">
            <el-table-column
                  label="用户ID"
                  prop="username">
            </el-table-column>
            <el-table-column
                  label="用户名称"
                  prop="teacher">
            </el-table-column>
            <el-table-column
                  label="用户密码">
                  <template slot-scope="scope">
                    <span v-if="scope.row.role">{{scope.row.password}}</span>
                    <span v-else>{{"******"}}</span>
                </template>
            </el-table-column>
            <el-table-column
                  label="用户角色"
                  prop="role">
            </el-table-column>
            <el-table-column
              align="right">
              <template slot="header">
                  <el-tag type="success">用户信息管理功能模块选择区域</el-tag>
              </template>
              <template slot-scope="scope">
                <el-button size="mini" type="danger" plain @click="look(scope.$index, scope.row)">查看</el-button>
                <el-button size="mini" @click="dialogFormVisible = true" type="primary" plain>增加</el-button>
                <el-button size="mini" type="danger" plain @click="handleDelete(scope.$index, scope.row)">移除</el-button>
                <el-dialog title="课程增加" :visible.sync="dialogFormVisible">
                  <el-form :model="addform">
                    <el-form-item label="用户ID" :label-width="formLabelWidth">
                      <el-input v-model="addform.username" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="用户名称" :label-width="formLabelWidth">
                      <el-input v-model="addform.teacher" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="用户密码" :label-width="formLabelWidth">
                      <el-input v-model="addform.password" autocomplete="off"></el-input>
                    </el-form-item>
                  </el-form>
                  <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addSubmitForm">确 定</el-button>
                  </div>
                </el-dialog>
              </template>
            </el-table-column>
          </el-table>
      </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      tableData: [],
      isshow: '1',
      addform: {
        username: '',
        teacher: '',
        password: ''
      },
      search: '',
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },
  async created () {
    //  this.getMenuList()
    const res = await this.$http.get('/user/findAll')
    console.log(res)
    this.tableData = res.data.datas.users
    // this.$router.push('/personalmain')
  },
  methods: {
    handleDelete (index, row) {
      this.$http.post(
        '/user/removeById',
        this.tableData[index].id,
        { 'Content-Type': 'application/json' }
      ).then(res => {
        if (res.data.code === 200) {
          this.$message.success('删除用户成功')
          this.tableData.splice(index, 1)
        }
      })
    },
    addSubmitForm () {
      this.$http.post(
        '/user/addUser',
        { 'Content-Type': 'application/json' }
      ).then(res => {
        if (res.data.code === 200) {
          this.$message.success('增加用户成功')
          this.tableData.push(this.addform)
        }
      })
    },
    look (index, row) {
      row.role = !row.role
      console.log(row.role)
    }
  }
}
</script>

<style lang="less" scoped>
.el-table{
  height: 60%;
  overflow: auto;
}
</style>
